// Vuex 状态管理
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)


const store = new Vuex.Store({
	// 数据源
	state: {
		authflag: uni.getStorageSync("_authflag") || {
			getUserInfo: false,
			getPhoneNumber: false,
			bindDouyin: false,
			hasShop: false // 用户名下是否有商品
		},
		userinfo: uni.getStorageSync("_userinfo") || {},
		historyLists: uni.getStorageSync("_search_history") || [],
		tokenRefreshTime: uni.getStorageSync("_token_refresh_time") || '',
		taskUserInfo: uni.getStorageSync("_task_user_info") || {},
	},
	mutations: {
		SET_USER_INFO(state, userinfo){
			state.userinfo = userinfo
		},
		SET_TASK_USER_INFO(state, taskUserInfo){
			state.taskUserInfo = taskUserInfo
		},
		SET_AUTH_FLAG(state, authflag){
			state.authflag = authflag
		},
		CLEAR_HISTORY_LIST(state){
			state.historyLists = []
		},
		SET_TOKEN_REFRESH_TIME(state, time){
			state.tokenRefreshTime = time
		}
	},
	actions: {
		// this.$store.dispatch('set_history', {
		// 	name: this.value
		// })
		set_authflag({commit, state}, value){
			let newAuthFlag = {
				...state.authflag,
				...value
			}
			uni.setStorageSync('_authflag', newAuthFlag)
			commit('SET_AUTH_FLAG', newAuthFlag)
		},
		set_userinfo({commit, state}, userinfo){
			let newuserinfo = {
				...state.userinfo,
				...userinfo
			}
			console.log(userinfo)
			uni.setStorageSync('_userinfo', newuserinfo)
			commit('SET_USER_INFO', newuserinfo)
		},
		set_task_userinfo({commit,state}, taskuserinfo){
			let newtaskuserinfo = {
				...state.taskUserInfo,
				...taskuserinfo
			}
			uni.setStorageSync('_task_user_info', newtaskuserinfo)
			commit('SET_TASK_USER_INFO', newtaskuserinfo)
		},
		clear_history({commit}){
			uni.removeStorageSync('_search_history')
			commit('CLEAR_HISTORY_LIST')
		},
		set_token_refresh_time({commit}, time){
			uni.setStorageSync('_token_refresh_time', time)
			commit('SET_TOKEN_REFRESH_TIME', time)
		}
	}
})

export default store